package org.flexharmony.harmonizer.javadef;

import static org.flexharmony.harmonizer.utils.CommonMethods.*;

import org.flexharmony.harmonizer.QualifiedTypeName;

/**
 * A Java type which is its qualified name and if it is an array or not.
 * 
 * @author Corey Baswell
 */
public class JavaType
{
  public final boolean isArray;
  
  public final QualifiedTypeName qName;
  
  public JavaType(String byteCodeType)
  {
    isArray = byteCodeType.contains("[");
    byteCodeType = byteCodeType.replaceAll("\\[", "");
    byteCodeType = convertByteCodeTypeName(byteCodeType);

    String packge;
    String name;
    if (byteCodeType.contains("."))
    {
      int index = byteCodeType.lastIndexOf('.');
      packge = byteCodeType.substring(0, index);
      name = byteCodeType.substring((index + 1), byteCodeType.length());
    }
    else
    {
      name = byteCodeType;
      packge = null;
    }
    
    qName = new QualifiedTypeName(packge, name);
  }
  
  public String toString()
  {
    String str = qName.toString();
    if (isArray) str += "[]";
    return str;
  }
  
  public boolean equals(Object obj)
  {
    if (obj instanceof JavaType)
    {
      JavaType javaType = (JavaType)obj;
      return ((isArray == javaType.isArray) && qName.equals(javaType.qName));
    }
    else
    {
      return false;
    }
  }
  
  public int hashCode()
  {
    return toString().hashCode();
  }
}
